var grid;
function initGrid(){
	grid = $("#maingrid").ligerGrid({
		url:HTR.U('index/banks/pageQuery'),
		pageSize:HTR.pageSize,
		pageSizeOptions:HTR.pageSizeOptions,
		height:'99%',
        width:'100%',
        minColToggle:6,
        rownumbers:true,
        columns: [
			{ display: '编号', name: 'bank_id', isSort: false},
			{ display: '图标', name: 'bank_pic', height: '300', isSort: false,render:function(rowdata, rowindex, value){
				return'<img src="'+HTR.conf.ROOT+'/'+value+'" height="28px" />';
			}},
			{ display: '分类', name: 'cat', isSort: false},
			{ display: '名称', name: 'bank_name', isSort: false},
			{ display: '地址', name: 'bank_url', isSort: false},
			{ display: '状态', name: 'bank_status', isSort: true,render:function(rowdata, rowindex, value){
				return '<span style="cursor:pointer;" onclick="toggleStatus('+rowdata["bank_id"]+','+value+');">'+((value=='1')?"隐藏":"显示")+'</span>';
			}},         
			{ display: '排序号', name: 'bank_sort', isSort: false,render:function(rowdata, rowindex, value){
				return '<span style="cursor:pointer;" ondblclick="changeSort(this,'+rowdata["bank_id"]+');">'+value+'</span>';
			}},
			{ display: '操作', name: 'op',isSort: false,render: function (rowdata, rowindex, value){
				var h = "";
				if(HTR.GRANT.YHGL_02)h += "<a href='"+HTR.U('index/banks/toEdit','id='+rowdata['bank_id'])+"'>修改</a> ";
				if(HTR.GRANT.YHGL_03)h += "<a href='javascript:toDel(" + rowdata['bank_id'] + ")'>删除</a> "; 
				return h;
			}}
        ]
    });
}
function toggleStatus(id,status){
	if(!HTR.GRANT.YHGL_02)return;
	var loading = HTR.msg('正在提交数据，请稍后...', {icon: 16,time:60000});
	$.post(HTR.U('index/banks/editStatus'),{id:id,status:status},function(data,textStatus){
		layer.close(loading);
		var json = HTR.toIndexJson(data);
		if(json.status=='1'){
			HTR.msg(json.msg,{icon:1});
			grid.reload();
		}else{
			HTR.msg(json.msg,{icon:2});
		}
	});
}
function toDel(id){
	var box = HTR.confirm({content:"您确定要删除该记录吗?",yes:function(){
	var loading = HTR.msg('正在提交数据，请稍后...', {icon: 16,time:60000});
		$.post(HTR.U('index/banks/del'),{id:id},function(data,textStatus){
			layer.close(loading);
			var json = HTR.toIndexJson(data);
			if(json.status=='1'){
				HTR.msg("操作成功",{icon:1});
				layer.close(box);
			    grid.reload();
			}else{
				HTR.msg(json.msg,{icon:2});
			}
		});
	}});
}

var oldSort;
function changeSort(t,id){
	$(t).attr('ondblclick'," ");
	var html = "<input type='text' id='sort-"+id+"' style='width:30px;' onblur='doneChange(this,"+id+")' value='"+$(t).html()+"' />";
	$(t).html(html);
	$('#sort-'+id).focus();
	$('#sort-'+id).select();
	oldSort = $(t).html();
}
function doneChange(t,id){
	var sort = ($(t).val()=='')?0:$(t).val();
	if(sort==oldSort){
		$(t).parent().attr('ondblclick','changeSort(this,'+id+')');
		$(t).parent().html(parseInt(sort));
		return;
	}
	$.post(HTR.U('index/banks/changeSort'),{id:id,bank_sort:sort},function(data){
		var json = HTR.toIndexJson(data);
		if(json.status==1){
			$(t).parent().attr('ondblclick','changeSort(this,'+id+')');
			$(t).parent().html(parseInt(sort));
		}
	});
}


		
//查询
function loadGrid(){
	var query = HTR.getParams('.query');
    grid.set('url',HTR.U('index/banks/pageQuery',query));
}

function editInit(){
  //文件上传
	HTR.upload({
  	  pick:'#adFilePicker',
  	  formData: {dir:'goods'},
      compress:false,//默认不对图片进行压缩
  	  accept: {extensions: 'gif,jpg,jpeg,bmp,png',mimeTypes: 'image/*'},
  	  callback:function(f){
  		  var json = HTR.toIndexJson(f);
  		  if(json.status==1){
  			$('#uploadMsg').empty().hide();
        var html = '<img src="'+HTR.conf.ROOT+'/'+json.savePath+json.thumb+'" />';
        $('#preview').html(html);
        // 图片路径
        $('#bank_pic').val(json.savePath+json.thumb);
  		  }
	  },
	  progress:function(rate){
	      $('#uploadMsg').show().html('已上传'+rate+"%");
	  }
    });
  

 /* 表单验证 */
    $('#adsForm').validator({
            fields: {
                bank_name: {
                  rule:"required;",
                  msg:{required:"银行名称不能为空"},
                  tip:"请输入银行名称",
                  ok:"验证通过",
                },
                bank_pic: {
                  rule:"required;",
                  msg:{required:"请上传银行图片"},
                  tip:"请上传银行图片",
                  ok:"",
                },
                bank_url: {
                  rule:"required",
                  msg:{required:"请选择银行地址"},
                  ok:"验证通过",
                }
            },
          valid: function(form){
            var params = HTR.getParams('.ipt');
            var loading = HTR.msg('正在提交数据，请稍后...', {icon: 16,time:60000});
            $.post(HTR.U('index/banks/'+((params.bank_id==0)?"add":"edit")),params,function(data,textStatus){
              layer.close(loading);
              var json = HTR.toIndexJson(data);
              if(json.status=='1'){
                  HTR.msg("操作成功",{icon:1});
                  location.href=HTR.U('Index/banks/index');
              }else{
                    HTR.msg(json.msg,{icon:2});
              }
            });
      }
    });
}